package wiki.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import wiki.generic.contents.Content;
import wiki.generic.io.Element;
import wiki.generic.utilities.AlreadyExistingElementException;
import wiki.generic.utilities.PageNotFoundException;
import wiki.setup.Setup;
/**
 * 
 * @author <p><strong>JwTeam</strong></p> 
 * Responsabile dell'inserimento di file scaricabili
 */
public class DownloadablesServlet extends HttpServlet{

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		HttpSession ses=req.getSession(false);
		if(ses!=null){
			boolean isDoc=(Boolean) ses.getAttribute("rights");
				if(isDoc){	
					boolean modify=req.getParameterMap().containsKey("name");
					String name=new String();
					if(modify){
						name=req.getParameter("name");
					}
					PrintWriter out=resp.getWriter();
					out.write("<html>\n");
					out.write("<head>\n");
					out.write("<title>Download File</title>\n");
					out.write("<link rel='stylesheet' type='text/css' href='./html/edit/scegliaudio.css'/>\n");
					out.write("<script type='text/JavaScript' src='./html/edit/scegliaudio.js'></script>\n");
					out.write("</head>\n");
					out.write("<body>\n");
					out.write("<div id='title'>\n");
					if(!modify){
						out.write("Scegli File Scaricabile\n");
					} else {
						out.write("Modifica File Scaricabile\n");
					}
					out.write("</div>\n");
					out.write("<div id='container'>\n");
					if(!modify){
						out.write("<form action='./down?type=down' method='post' name='modulo' id='modulo'>\n");
					} else {
						out.write("<form action='./down?type=down&nome="+name+"&mod=mod' method='post' name='modulo' id='modulo'>\n");
					}
					out.write("<table>\n");
					out.write("<div id='show_message'>I seguenti campi sono obbligatori*:</div>\n");
					out.write("<tr>\n");
					if(!modify){
						out.write("<td class='col1'>Nome<span id='star_nome'>*</span>:&nbsp;&nbsp;&nbsp;");
						out.write("<input class='testo' type='text' name='nome' value=''/></td>\n");
					}
					out.write("</tr>\n");
					out.write("</table>\n");
					out.write("<h3 id='h3Color'>Scegli un file scaricabile:</h3>\n");
					out.write("<ul>\n");
					String img="<img src='./html/immagini/download.png' class='imgdown'/>&nbsp;";
					HashMap<Integer, Element> map=Setup.getSetup().getMediator().getVservice().getElementsMapbyType("down");
					Set<Integer> keys=map.keySet();
					if(map.size()==0){
						resp.sendRedirect("./html/errorpages/nodownloadcaricati.html");
					}
					Content c=null;
					if(modify){
						String corso=(String) ses.getAttribute("corso");
						String current=(String) ses.getAttribute("current");
						try {
							c=Setup.getSetup().getMediator().getpService().getContentbyName(corso, current, name);
						} catch (PageNotFoundException e) {
							resp.sendRedirect("./html/errorpages/pagina404.html");
						}
					}
					
					boolean first=true;
					for (Iterator<Integer> iterator = keys.iterator(); iterator.hasNext();) {
						Integer ID = iterator.next();
						out.write("<li> <input type='radio' value='"+ID+"' name='cont'");
						if(first || (modify && c.getID()==ID)){
							out.write("checked='checked'");
						}
						out.write("/>");
						out.write(img);
						out.write(Setup.getSetup().getMediator().getGpService().getFileName(map.get(ID).getInfo())+"<em style='font-size:10px'>["+map.get(ID).getOwner()+"]</em>");
						out.write("</li>\n");
						first=false;
					}
					out.write("</ul>\n");
					out.write("<br />\n");
					out.write("<h3 id='h3Color'>Oppure caricane uno nuovo:</h3>\n");
					out.write("<br/>\n");
					if(!modify){
					out.write("<a href='./upload?down=down'><input type='button' value='Upload' id='bottone_upload' name='but'/></a>\n");
					} else{
						out.write("<a href='./upload?down=down&mod=mod&name="+c.getContentName()+"&ID="+c.getID()+"&type=down'><input type='button' value='Upload' id='bottone_upload' name='but'/></a>\n");
					}
					out.write("<br />");
					if(!modify){
						out.write("<input type='submit' value='Aggiungi' id='bottone' name='submit' onclick=\"if(controllaVuoti('modulo','star_nome','show_message')!=true){return false;}\" />\n");
					} else{
						out.write("<input type='submit' value='Aggiungi' id='bottone' name='submit'/>\n");
					}
					out.write("</form>");
					out.write("</div>");
					out.write("</body>");
					out.write("</html>");
			} else {
				resp.sendRedirect("./html/errorpages/nopermessi.html");
				return;
			}
		} else {
				resp.sendRedirect("./html/LOGIN/login.html");
				return;
		}
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		HttpSession ses=req.getSession(false);
	if(ses!=null){
			boolean isDoc=(Boolean) ses.getAttribute("rights");
		if(isDoc){
				String corso=(String) ses.getAttribute("corso");
				String type=req.getParameter("type");
				if(type!=null){
					String nome=req.getParameter("nome");
					if(nome==null || nome.equals("")){
						nome="___";
					}
					nome=nome.replace(" ", "_");
					nome=Setup.getSetup().getMediator().getGpService().changeInputText(nome);
					int ID=Integer.parseInt(req.getParameter("cont"));
					String current=(String) ses.getAttribute("current");
					
					try {
						if(!req.getParameterMap().containsKey("mod")){
							Setup.getSetup().getMediator().geteService().appendContent(corso, current, nome, type, ID, new ArrayList<String>());
						} else {
							Setup.getSetup().getMediator().geteService().modifyContent(corso, current, nome, ID, new ArrayList<String>());
						}
						resp.sendRedirect("./view?page="+current);
					} catch (AlreadyExistingElementException e) {
						resp.sendRedirect("./html/errorpages/nomecontenuto.html"); 
					} catch (PageNotFoundException e) {
						resp.sendRedirect("./html/errorpages/pagina404.html");
					}
					
				}
		} else {
			resp.sendRedirect("./html/errorpages/nopermessi.html");
			return;
		}
	} else {
		resp.sendRedirect("./html/LOGIN/login.html");
		return;
	}
}
	
	
	
	
}
